package com.ruoyi.web.ydz.util.quartz;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.ydz.domain.MedicationReminder;
import com.ruoyi.web.ydz.service.impl.MedicationReminderServiceImpl;
import org.quartz.JobDataMap;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Component
public class quartzRestore {

    @Resource
    private MedicationReminderServiceImpl medicationReminderService;
    @Resource
    private QuartServiceImpl quartService;

    /*服务重启时
    * 恢复用药提醒定时任务*/
//    @PostConstruct
    public void restore(){

        /*获取所有需要恢复的用药提醒*/
        List<MedicationReminder> restoreMedicationReminder = medicationReminderService.getRestoreMedicationReminder();
        if (!Objects.isNull(restoreMedicationReminder)){
            restoreMedicationReminder.forEach(bean -> {
                JobDataMap jobDataMap = new JobDataMap();
                jobDataMap.put("medicationReminderId", bean.getId());
                R r = quartService.addCronJob("jobname" + bean.getId(), quartzUtil.dateToCron(bean.getRemindTime()), "com.ruoyi.web.ydz.util.quartz.TaskJob", "triggername" + bean.getId(), jobDataMap);
            });
        }
        System.out.println(restoreMedicationReminder.size() + "定时任务恢复完成");
    }
}
